package com.chaber.containers.views;


import com.chaber.containers.beans.ProductCategoryData;

import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.Tab;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.util.Callback;

public class ProdCatListView extends Tab {

	private TreeView<ProductCategoryData> prodCategoriesTree;
	
	//action buttons
	private Button addButton;
	private Button removeButton;
	
	public ProdCatListView() {
		super("Product categories");
		
		BorderPane contentPane = new BorderPane();
		this.prodCategoriesTree = this.buildTree();
		contentPane.setCenter(this.prodCategoriesTree);
		
		HBox buttonsBox = this.buildActionButtons();
		contentPane.setTop(buttonsBox);
		
		this.setContent(contentPane);
	}

	private HBox buildActionButtons() {
		HBox buttonsBox = new HBox();
		buttonsBox.setPadding(new Insets(20));
		buttonsBox.setSpacing(10);
		
		this.addButton = new Button("Add");
		buttonsBox.getChildren().add(addButton);
		
		this.removeButton = new Button("Remove");
		buttonsBox.getChildren().add(removeButton);
		
		this.deactivateActionButtons();
		
		return buttonsBox;
	}

	public TreeView<ProductCategoryData> buildTree() {
		ProductCategoryData rootProCat = new ProductCategoryData();
		rootProCat.setName("Product categories");
		TreeItem<ProductCategoryData> rootItem = new TreeItem<ProductCategoryData>(rootProCat);
		rootItem.setExpanded(true);
		
		TreeView<ProductCategoryData> tree = new TreeView<ProductCategoryData>(rootItem);
		tree.setCellFactory(new Callback<TreeView<ProductCategoryData>, TreeCell<ProductCategoryData>>() {
			@Override
			public TreeCell<ProductCategoryData> call(TreeView<ProductCategoryData> treeView) {
				TreeCell<ProductCategoryData> customTreeCell = new TreeCell<ProductCategoryData>() {
					
					@Override
					protected void updateItem(final ProductCategoryData pCategoryData, boolean empty) {
						super.updateItem(pCategoryData, empty);

						if (!empty && pCategoryData != null) {
							setText(pCategoryData.getFullName());
						} else {
							setText(null);
							setGraphic(null);
						}
					}
				};
				
				return customTreeCell;
			}
		});
		
		tree.setEditable(false);
		tree.setVisible(true);
		
		return tree;
	}
	
	public void deactivateActionButtons() {
		this.addButton.setDisable(true);

		this.removeButton.setDisable(true);
	}
	
	public void activateActionButtons() {
		this.addButton.setDisable(false);
		
		this.removeButton.setDisable(false);
	}
	
	public TreeView<ProductCategoryData> getProdCategoriesTree() {
		return prodCategoriesTree;
	}

	public void setProdCategoriesTree(TreeView<ProductCategoryData> prodCategoriesTree) {
		this.prodCategoriesTree = prodCategoriesTree;
	}

	public BorderPane getContentPane() {
		return (BorderPane) this.getContent();
	}

	public Button getAddButton() {
		return addButton;
	}

	public void setAddButton(Button addButton) {
		this.addButton = addButton;
	}

	public Button getRemoveButton() {
		return removeButton;
	}

	public void setRemoveButton(Button removeButton) {
		this.removeButton = removeButton;
	}
}
